<?php
if (!defined('ROOT_PATH')) die('No direct access.');
function openToko($fb,$page,$nama,$alamat,$desc){
	$db = new db();
	$slug = slugging($nama);
	$tgl = date('Y-m-d H:i:s', time());
	$query = "UPDATE Pengguna SET tgl_reg ='$tgl', `isToko`=1,`fb_page-toko`='$page',`nama-toko`='$nama',`slug_nama-toko`='$slug',`alamat-toko`='$alamat',`deskripsi-toko`='$desc',`poin_rating-toko`=0  WHERE `idFB` = '$fb'";
	$db->exec($query);
	$ret = $db->rowCount();
	if ($ret==0)
		$_SESSION['message'] = "Gagal buka toko";
	else
		$_SESSION['message'] = "Sukses buka toko";
}

function editToko($fb,$page,$nama,$alamat,$desc){
	$db = new db();
	$slug = slugging($nama);
	$query = "UPDATE Pengguna SET `fb_page-toko`='$page',`nama-toko`='$nama',`slug_nama-toko`='$slug',`alamat-toko`='$alamat',`deskripsi-toko`='$desc',`poin_rating-toko`=0  WHERE `idFB` = '$fb'";
	$db->exec($query);
	$ret = $db->rowCount();
	if ($ret==0)
		$_SESSION['message'] = "Gagal ubah toko";
	else
		$_SESSION['message'] = "Sukses ubah toko";
}

function addBarang($id,$nama,$berat,$harga,$desc,$pre,$tag,$kategori){
	$db = new db();
	$slug = slugging($nama);
	$tgl = date('Y-m-d H:i:s', time());
	$query = "INSERT INTO Barang (`idPenjual`,`tglReg`,`nama`,`slug-nama`,`berat`,`harga`,`deskripsi`,`terjual`,`poin_rating`,`preorder`,`keywords`,`kategori`) VALUES ($id,'$tgl','$nama','$slug',$berat,$harga,'$desc',0,0,$pre,'$tag',$kategori)";
	$ret = $db->exec($query);
	$ret = $db->rowCount();
	if ($ret==0)
		$_SESSION['message'] = "Gagal tambah barang";
	else
		$_SESSION['message'] = "Sukses tambah barang";
}

function cekSdhBeli($pembeli,$penjual){
	$ret = true;
	$db = new db();
	$query = "SELECT count(*) FROM ";
	return $ret;
}

function getBarangToko($penjual,$tags){
	//$db = new db();
	$query = "SELECT * FROM Barang WHERE idPenjual = $penjual ";
	if (count($tags)>0){
		$query .= "AND (";
		for ($i=0; $i<(count($tags)-1); $i++){
			$query .= "keywords LIKE '%".$tags[$i]."%' OR ";
			$query .= "nama LIKE '%".$tags[$i]."%' OR ";
		}
		$query .= "keywords LIKE '%".$tags[$i]."%' OR ";
		$query .= "nama LIKE '%".$tags[$i]."%' ";
		$query .= ") ";
	}
	$query .= " ORDER BY tglReg DESC";
	//echo $query;
	//$ret = $db->query($query);
	return $query;
}

function getArtikelnya($idtoko){
	$db = new db();
	$query = "SELECT tanggal,judul,isi,tag,nama,idArtikel,`slug-judul`,type,idReview,idFB FROM `Artikel` INNER JOIN Pengguna ON idPublisher = idPengguna WHERE idReview = '$idtoko' AND type=1";
	$ret = $db->query($query);
	return $ret[0];
}
